package com.cupcorn.balapkarung.state;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.entity.Entity;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;

import android.view.KeyEvent;

import com.cupcorn.balapkarung.GameDefine;
import com.cupcorn.balapkarung.GameDefineText;
import com.cupcorn.balapkarung.LocalData;
import com.cupcorn.lib.Anchor;
import com.cupcorn.lib.define.iDefines;
import com.cupcorn.lib.display.DisplaySprite;
import com.cupcorn.lib.display.DisplayText;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameState;
import com.cupcorn.onlinescript.OnlineScript;

public class StateHighscore extends GameState implements GameDefine, GameDefineText
{
	
	DisplaySprite	displaySprite;
	DisplayText		displayText;
	boolean			upinside;
	final int		animateStep	= MAIN_MENU_ANIMATION_STEP;
	int				animateCount;
	boolean			ready, quitingSubmenu;
	Entity			layerMenu;
	public static boolean waitForHighscoreOnline, highscoreOnlineReady, highscoreNotOnline;
	
	public StateHighscore(GameEngine engine)
	{
		super(engine);
	}
	
	public void load()
	{
		displayText = new DisplayText(engine, engine.displayFont, TEXT);
		displaySprite = new DisplaySprite(engine, GAME_MENU_HIGHSCORE);
		for(int i = 0; i < GAME_MENU_HIGHSCORE.length; i++)
		{
			displaySprite.newSprite(i, engine.hud, false);
		}
		layerMenu = new Entity();
		layerMenu.attachChild(displaySprite.getSprite(HIGH_BINGKAI_BIRU));
		displaySprite.getSprite(HIGH_BINGKAI_BIRU).addRectangularShape(displaySprite.getSprite(HIGH_OFFLINE), 0, 50, Anchor.TOP_CENTER);
		displaySprite.getSprite(HIGH_BINGKAI_BIRU).addRectangularShape(displaySprite.getSprite(HIGH_ONLINE), 0, -50, Anchor.BOTTOM_CENTER);
		displaySprite.getSprite(HIGH_OFFLINE).addText(displayText.text(TEXT_HIGHSCORE_OFFLINE));
		displaySprite.getSprite(HIGH_ONLINE).addRectangularShape(displaySprite.getSprite(HIGH_GO_BUTTON), Anchor.CENTER);	
		displaySprite.getSprite(HIGH_ONLINE).addRectangularShape(displaySprite.getSprite(HIGH_GO_BUTTON_PRESSED), Anchor.CENTER);		
	}
	
	public void init()
	{
		displayText.text[TEXT_HIGHSCORE_OFFLINE].setText("" + +LocalData.highscoreOffline + " Meter");
		displaySprite.getSprite(HIGH_OFFLINE).setTextPosition(displaySprite.getSprite(HIGH_OFFLINE).text, 0, 20, Anchor.CENTER);
		displaySprite.getSprite(HIGH_GO_BUTTON_PRESSED).setVisible(false);
		upinside = false;
		animateCount = 0;
		ready = false;
		quitingSubmenu = false;
		engine.hud.attachChild(layerMenu);
		layerMenu.setY(-GameEngine.CAMERA_HEIGHT);
	}
	
	public void update(float deltaTime)
	{
		if(waitForHighscoreOnline)
		{
			if(highscoreOnlineReady)
			{
				engine.nextState(STATE_HIGHSCORE_ONLINE, iDefines.MESSAGE_STATE_INIT);
				highscoreOnlineReady = false;
				waitForHighscoreOnline = false;
			}
			else if(highscoreNotOnline)
			{
				waitForHighscoreOnline = false;
				highscoreNotOnline = false;
			}
			return;
		}
		if (quitingSubmenu)
		{
			if (animateBackTransition())
			{
				engine.nextState(STATE_MAIN_MENU, iDefines.MESSAGE_STATE_UPDATE);
				removeAllObject();
			}
			return;
		}
		if (ready)
		{
			upinside = false;
			return;
		}
		if (animateTransition())
		{
			ready = true;
		}
	}
	
	public void exit()
	{
	}
	
	public boolean keyUp(int keyCode, KeyEvent event)
	{
		if(quitingSubmenu || waitForHighscoreOnline || !ready)
		{
			return false;
		}
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			quitingSubmenu = true;
			animateCount = 0;
		}
		return false;
	}
	
	public void sceneTouch(Scene scene, TouchEvent touchEvent)
	{
		if (!ready || quitingSubmenu) { return; }
		if (touchEvent.isActionUp() && !upinside)
		{
			quitingSubmenu = true;
			animateCount = 0;
		}
	}
	
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if (pSceneTouchEvent.isActionUp())
		{
			if (pTouchArea.equals(displaySprite.getSprite(0)))
			{
				upinside = true;
			}
			if(pTouchArea.equals(displaySprite.getSprite(HIGH_GO_BUTTON_PRESSED)) && !waitForHighscoreOnline)
			{

				displaySprite.getSprite(HIGH_GO_BUTTON).setVisible(true);
				displaySprite.getSprite(HIGH_GO_BUTTON_PRESSED).setVisible(false);
				OnlineScript.get_TopScores();
				waitForHighscoreOnline = true;
			}
		}
		
		if (pSceneTouchEvent.isActionDown())
		{
			if(pTouchArea.equals(displaySprite.getSprite(HIGH_GO_BUTTON)) && !waitForHighscoreOnline)
			{
				displaySprite.getSprite(HIGH_GO_BUTTON).setVisible(false);
				displaySprite.getSprite(HIGH_GO_BUTTON_PRESSED).setVisible(true);
			}
		}
		
		return false;
	}
	
	public void click(ClickDetector clickDetector, int pointerID, float sceneX, float sceneY)
	{
	}
	
	public void scrollStarted(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{
	}
	
	public void Scroll(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{
	}
	
	public void scrollFinished(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{
	}
	
	private void removeAllObject()
	{
		EngineLock lock = engine.getEngine().getEngineLock();
		lock.lock();
		layerMenu.detachSelf();
		lock.unlock();
	}
	
	boolean animateTransition()
	{
		StateMainMenu.layerSubMenu.setY(StateMainMenu.layerSubMenu.getY() + StateMainMenu.farMove);
		StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).setY(StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).getY() + StateMainMenu.farMove);
		layerMenu.setY(layerMenu.getY() + StateMainMenu.farMove);
		if (animateCount == animateStep) { return true; }
		animateCount++;
		return false;
	}
	
	boolean animateBackTransition()
	{
		StateMainMenu.layerSubMenu.setY(StateMainMenu.layerSubMenu.getY() - StateMainMenu.farMove);
		StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).setY(StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).getY() - StateMainMenu.farMove);
		layerMenu.setY(layerMenu.getY() - StateMainMenu.farMove);
		if (animateCount == animateStep) { return true; }
		animateCount++;
		return false;
	}
}